package DataStructure.arrayAndList.list;


/**
 * @author liujun
 * @version 1.0
 * @date 2020/11/1
 * @author—Email ljfirst@mail.ustc.edu.cn
 * @blogURL https://blog.csdn.net/ljfirst
 * @description 链表结构（单双链表）、跳表
 */

public class Nodelj {

    public int value;
    public Nodelj next;
    public Nodelj front;
    public Nodelj down;

    // 单链表
    public Nodelj() {
        this.value = Integer.MIN_VALUE;
        this.next = null;
        this.front = null;
        this.down = null;
    }

    // 单链表、双向链表、跳表
    public Nodelj(int value) {
        this.value = value;
        this.next = null;
        this.front = null;
        this.down = null;
    }

    // 根据数组生成链表
    public Nodelj String2Nodelj(String array) {
        if (array == null || array.length() == 0) {
            return null;
        }
        Nodelj root = new Nodelj(array.charAt(0) - '0');
        Nodelj first = root;
        for (int i = 1; i < array.length(); i++) {
            root.next = new Nodelj(array.charAt(i) - '0');
            root = root.next;
        }
        return first;
    }

    public String Nodelj2String(Nodelj node) {
        if (node == null) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        while (node != null) {
            sb.append(node.value);
            node = node.next;
        }
        String valueStr = sb.toString();
        return valueStr;
    }

    // 根据数组生成链表
    public static Nodelj Array2Nodelj(int[] array) {
        if (array == null || array.length == 0) {
            return null;
        }
        Nodelj root = new Nodelj(array[0]);
        Nodelj first = root;
        for (int i = 1; i < array.length; i++) {
            root.next = new Nodelj(array[i]);
            root = root.next;
        }
        return first;
    }

    // 链表转数组
    public static int[] Nodelj2Array(Nodelj root) {
        if (root == null) {
            return new int[]{};
        }
        int nodeLength = 0;
        Nodelj ahead = root;
        while (root != null) {
            nodeLength++;
            root = root.next;
        }
        int[] array = new int[nodeLength];
        for (int i = 0; i < nodeLength; i++) {
            // 防止意外的其他并发操作了Node导致空指针
            if (ahead == null) {
                return null;
            }
            array[i] = ahead.value;
            ahead = ahead.next;
        }
        return array;
    }

    private int Nodelj2Int(Nodelj node) {
        if (node == null) {
            return 0;
        }
        StringBuffer sb = new StringBuffer();
        while (node != null) {
            sb.append(node.value);
            node = node.next;
        }
        String valueStr = sb.toString();
        return Integer.parseInt(valueStr);
    }

    private Nodelj Int2Nodelj(int a) {

        if (a == 0) {
            return new Nodelj(0);
        }
        String arrayStr = String.valueOf(a);
        return String2Nodelj(arrayStr);
    }

    // 打印链表
    public void print(Nodelj root) {
        System.out.println();
        if (root == null) {
            return;
        }
        while (root != null) {
            System.out.print(root.value + " ");
            root = root.next;
        }
    }

    public boolean equals(Nodelj first, Nodelj second) {
        if (first == null && second == null) {
            return true;
        }
        if (first == null || second == null) {
            return false;
        }
        while (first != null && second != null) {
            if (first.value != second.value) {
                return false;
            }
            first = first.next;
            second = second.next;
        }
        return first == null && second == null;
    }

    public boolean equals(Nodelj root) {
        return equals(this, root);
    }
}
